package com.scenic.rd_android.testview.webview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.AttributeSet;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebIconDatabase;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.nova.scenic.projectlibs.util.debug.MyLog;

/**
 * Created by scenic on 2018/1/13.
 */

public class TestWebView extends WebView {
    static MyLog myLog = new MyLog("TestWebView");

    public TestWebView(Context context) {
        super(context);
        init();
    }

    public TestWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TestWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    public void onResume() {
        super.onResume();

        myLog.info("onResume");
    }


    private void init() {

        //设置不用系统浏览器打开,直接显示在当前Webview
        this.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                myLog.info("shouldOverrideUrlLoading " + url );
                view.loadUrl(url);
                HitTestResult hit = view.getHitTestResult();
                if (hit != null  ) {
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(url);
                    intent.setData(content_url);
                    getContext().startActivity(intent);
                    goBack();
                    return true;
                } else {
                    view.loadUrl(url);
                    return true;
                }


            }
        });

        //设置WebChromeClient类
        this.setWebChromeClient(new WebChromeClient() {


            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                myLog.info("the title: " + title);
            }


            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                myLog.info("progress " + newProgress);
            }
        });


        //设置WebViewClient类
        this.setWebViewClient(new WebViewClient() {
            //设置加载前的函数
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                System.out.println("开始加载了");
                myLog.info("onPageStarted " + url + "  " + favicon);

            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {
                myLog.info("onPageFinished " + url);

            }
        });



        initWebSettings();

    }


    private void initWebSettings() {



        //web setting


        WebSettings ws = getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setDomStorageEnabled(true);
        ws.setDatabaseEnabled(true);
        WebIconDatabase.getInstance().open(getContext().getDir("icons", Context.MODE_PRIVATE).getPath());
        String databasePath = getContext().getDir("database", Context.MODE_PRIVATE).getPath();
        ws.setDatabasePath(databasePath); // 重要，必须设置
        ws.setLoadWithOverviewMode(true);
        // 提高渲染的优先级
        ws.setRenderPriority(WebSettings.RenderPriority.HIGH);// 把图片加载放在最后来加载渲染
        ws.setAppCacheEnabled(true);
        ws.setCacheMode(WebSettings.LOAD_DEFAULT);
        ws.setDefaultTextEncodingName("UTF-8");// 不设置系统默认“latin”，给个gbk



        setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        getSettings().setUseWideViewPort(true);



        setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                myLog.info("download " + url + " userAgent " + userAgent + " contentDisposition  " + contentDisposition + "  mimetype " + mimetype);
            }
        });
//        ws.setBlockNetworkImage(true);



    }
}
